{extend name="layout/layout" /}

{block name="content"}
<div class="row">
    <div class="col-lg-12">
        <div class="box box-success">
            <!--<div class="box-header with-border">
                <h3 class="box-title">个人中心</h3>
                <div class="box-tools pull-right">

                </div>
            </div>-->
            <div class="box-body">
                <form action="" method="PUT" id="form_role_new" class="form-horizontal">
                    <div class="form-horizontal" id="form_me_update">
                        <div class="form-group">
                            <lable class="col-md-3 control-label"> <span class="required"> * </span>用户账号：</lable>
                            <div class="col-md-5">

                                <input type="text" class="form-control input-medium" name="loginName" value="{$admin.loginName}" readonly>
                            </div>
                        </div>
                        <div class="form-group">
                            <lable class="col-md-3 control-label"> <span class="required"> * </span>姓名：</lable>
                            <div class="col-md-5">
                                <input type="text" class="form-control input-medium" name="name" placeholder="" maxlength="20" value="{$admin.name}">
                            </div>
                        </div>
                        <div class="form-group">
                            <lable class="col-md-3 control-label"> <span class="required"> * </span>手机：</lable>
                            <div class="col-md-5">
                                <input type="text" class="form-control input-medium" name="phone" placeholder="" maxlength="11" value="{$admin.phone}">
                            </div>
                        </div>

                    </div>

                    <hr />

                    <div class="form-actions">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label class="col-md-3 control-label"></label>
                                    <div class="col-md-9">
                                        <button type="button" id="btn_new_role" class="btn btn-primary">
                                            保存 <i class="fa fa-floppy-o"></i>
                                        </button>
                                        <button type="button" id="btn_new" class="btn btn-warning">
                                            修改密码
                                        </button>
                                        <span class="help-block "></span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<!-- 修改密码-弹框 -->
<div class="modal fade" id="modal_edit_pass" data-backdrop="static" data-keyboard="false">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                </button>
                <h4 class="modal-title">修改密码</h4>
            </div>
            <form action="" method="PUT" id="form_password" class="form-horizontal">
                <div class="modal-body">
                    <div class="form-horizontal">
                        <div class="form-group">
                            <div class="col-md-9 text-center">
                                <span class="help-block"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <lable class="col-md-3 control-label"> <span class="required"> * </span>旧密码：</lable>
                            <div class="col-md-9">
                                <input type="password" maxlength="20" class="form-control input-medium" name="password" placeholder="请输入旧密码">
                            </div>
                        </div>
                        <div class="form-group">
                            <lable class="col-md-3 control-label"> <span class="required"> * </span>新密码：</lable>
                            <div class="col-md-9">
                                <input type="password" maxlength="20" class="form-control input-medium" name="password1" placeholder="请输入新密码">
                            </div>
                        </div>
                        <div class="form-group">
                            <lable class="col-md-3 control-label"> <span class="required"> * </span>确认密码：</lable>
                            <div class="col-md-9">
                                <input type="password" maxlength="20" class="form-control input-medium" name="password2" placeholder="请再输入一次">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-info" id="modal_sure_new">确定</button>
                    <button type="button" class="btn default" data-dismiss="modal">取消</button>
                </div>
            </form>
        </div>
    </div>
</div>
{/block}

{block name="pagescript"}
<script src="__STATIC__/admin/js/include.js" type="text/javascript"></script>
<script>
    var post_url = "{:url('admin/Center/editPost')}";
    var passwd_url = "{:url('admin/Center/editPasswd')}";

    // 表单验证
    function validate(value, type) {
        // 非空验证
        if (type === 'null') {
            return value.indexOf(' ')>0;
        }
        // 字段值
        var value = $.trim(value);

        // 非空验证
        if (type === 'require') {
            return !!value;
        }


        //汉字验证
        if(type === 'ecode'){
            return  /^[^\u4e00-\u9fa5]{0,}$/.test(value);
        }
        // 手机号验证
        if (type === 'phone') {
            return /^1[0-9]{10}$/.test(value);
            // return /^1[3|4|5|8][0-9]{9}$/.test(value);
        }
        // 邮箱验证
        if (type === 'email') {
            return /^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/.test(value);
        }
    };

    //修改基本信息-提交
    $('#btn_new_role').click(function() {
        var _data = $("#form_role_new").serializeObject();
        // console.log(_data);
        if (!validate(_data.name, 'require')) {
            $('#form_role_new .help-block').html('请填写姓名').attr('style','color:red');
            return;
        }
        if (!validate(_data.phone, 'require')) {
            $('#form_role_new .help-block').html('请填写电话').attr('style','color:red');
            return;
        }
        if (!validate(_data.phone, 'phone')) {
            $('#form_role_new .help-block').html('电话格式不正确，请修改').attr('style','color:red');
            return;
        }else {
            var param=_data;
            $.ajax({
                url : post_url,
                type : 'post',
                dataType : 'json',
                contentType:"application/json; charset=utf-8",
                data : JSON.stringify(param),
            }).done(function(data) {
                if (data.code === '1') {   // 正确
                    $('#form_role_new .help-block').html('修改成功').attr('style','color:green');
                    location.reload();
                } else if (data.code === '2') { // 错误
                    $('#form_role_new .help-block').html(data.message).attr('style','color:red'); // 返回错误信息
                }
            });
        }
    });

    //修改密码-弹窗
    $('#btn_new').click(function() {
        $('#modal_edit_pass').modal('show');
    });

    //修改密码-提交
    $('#modal_sure_new').click(function() {
        var _data = $("#form_password").serializeObject();
        if (!mdTool.validate(_data.password, 'require')) {
            $('#modal_edit_pass .help-block').html("请输入密码").css('color','red');
            return;
        }if (mdTool.validate(_data.password1, 'null')) {
            $('#modal_edit_pass .help-block').html("新密码不能包含空格").css('color','red');
            return;
        }if (mdTool.validate(_data.password2, 'null')) {
            $('#modal_edit_pass .help-block').html("确认密码不能包含空格").css('color','red');
            return;
        }if (!mdTool.validate(_data.password1, 'require')) {
            $('#modal_edit_pass .help-block').html("请输入新密码").css('color','red');
            return;
        }if (!mdTool.validate(_data.password2, 'require')) {
            $('#modal_edit_pass .help-block').html("请再输一次新密码").css('color','red');
            return;
        }else {
            $.ajax({
                url: passwd_url,
                type: 'post',
                dataType: 'json',
                contentType:"application/json; charset=utf-8",
                data: JSON.stringify(_data),
            }).done(function(data) {
                if (data.code === '1') {
                    $('#modal_edit_pass .help-block').html("修改密码成功").css('color','green');
                    $('#modal_edit_pass').modal('hide');
                } else if (data.code === '2') {
                    $('#modal_edit_pass .help-block').html(data.message).css('color','red'); // 返回错误信息
                }
            });
        }
    });
    // 关闭弹框
    $(".modal").on('hidden.bs.modal', function(e) {
        $('.modal .help-block').html(''); // 清空提示
        $('.modal input').val(''); // 清空 input
        $('#form_me_update .help-block').html("");
    });

</script>
{/block}